home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wdj0797.zip / SHMIDT.ZIP / VW32SVC.ASM < prev    next >
Assembly Source File  |  1996-11-14  |  6KB  |  211 lines

  1.    title vw32svc
  2.    page ,132
  3.    
  4. .386p
  5.  
  6. .xlist
  7.    include vmm.inc
  8. .list
  9.  
  10. PDDB typedef ptr VxD_Desc_Block
  11.  
  12. ; opcode for the relative jump instruction for the jump stub
  13. JMP_JV  equ 0e9h
  14. ; W32 service jump stub structure
  15. W32STUB struc
  16.  Stub_Jmp   db  JMP_JV  ; relative jump opcode
  17.  Stub_targ  dd  ?       ; jump target
  18.  Stub_prev  dd  ?       ; address of the previous service handler
  19. W32STUB ends
  20.  
  21. ; the structure of a single Win32 service table entry
  22. W32ENTRY struc
  23.  W32_Handler    dd  ?   ; service handler
  24.  W32_ParamCnt   dd  ?   ; number of DWORD parameters
  25. W32ENTRY ends
  26.  
  27. VxD_CODE_SEG
  28.  
  29. ; Helper function: finds Win32 service table entry 
  30. ; for the VXDID + service
  31. ; returns: Win32 table entry pointer or 0 if an entry doesn't exist
  32. Get_Win32_Service_Table proc near C uses ecx, service:dword
  33.    mov     eax, service
  34.    ror     eax, 16
  35.    movzx   eax, ax
  36.    VMMCall Get_DDB
  37.  
  38.    xor     eax, eax    
  39.    .IF ecx
  40.       .IF [ecx].DDB_Flags & DDB_HAS_WIN32_SVCS
  41.          mov     ecx, [ecx].DDB_Win32_Service_Table
  42.          .IF ecx && ([ecx] != eax)   
  43.             ; win32 table exists and has services
  44.             movzx   eax, word ptr service
  45.             ; make sure requested service number
  46.             ; is not too big
  47.             .IF eax < [ecx]
  48.                lea     eax, [ecx+8+eax*8]
  49.             .ELSE
  50.                xor     eax, eax
  51.             .ENDIF          
  52.          .ENDIF
  53.       .ENDIF
  54.    .ENDIF
  55.    ret
  56. Get_Win32_Service_Table endp
  57.  
  58. Get_Win32_Param_Count proc near C public, service:dword
  59.    cCall   Get_Win32_Service_Table,<service>
  60.    .IF eax
  61.       mov     eax, [eax].W32_ParamCnt
  62.    .ENDIF
  63.    ret
  64. Get_Win32_Param_Count   endp
  65.  
  66. ; insert hookproc into a chain of VxD Win32 services
  67. ; returns a pointer to an address of the previous 
  68. ; service handler in eax or 0
  69. Hook_Win32_Service proc near C public,service:dword,hookproc:dword
  70. local   w32tab:dword
  71.    pushad
  72.     
  73.    cCall   Get_Win32_Service_Table,<service>
  74.    .IF eax
  75.       mov     w32tab, eax
  76.         
  77.       VMMCall _HeapAllocate,<<type W32STUB>,HEAPZEROINIT>
  78.       .IF !ZERO?
  79.          ; the allocated stub will become a service handler
  80.          ; it will contain only one instruction: 'JMP hookproc'
  81.          mov     [eax].Stub_Jmp, JMP_JV ; opcode for near jmp
  82.          sub     hookproc, eax
  83.          sub     hookproc,5            ; immediate
  84.          push    hookproc
  85.          pop     [eax].Stub_targ
  86.  
  87.          ; get previous handler
  88.          mov     edx, w32tab                     
  89.          mov     esi, [edx].W32_Handler
  90.          mov     [eax].Stub_prev, esi
  91.          ; make the stub the new handler         
  92.          mov     [edx].W32_Handler, eax
  93.          ; return a pointer to the previous handler's address
  94.          lea     eax, [eax].Stub_prev
  95.       .ENDIF
  96.    .ENDIF
  97.     
  98.    mov     [esp].Pushad_EAX, eax
  99.    popad
  100.    ret
  101. Hook_Win32_Service  endp
  102.  
  103. ; remove hookproc from the service chain
  104. ; returns eax != 0 if success
  105. Unhook_Win32_Service proc near C public,service:dword,hookproc:dword
  106.    pushad
  107.                                   
  108.    cCall   Get_Win32_Service_Table,<service>
  109.    .IF eax     ; is service valid?
  110.       ; get current handler's stub address
  111.       mov     edx, [eax].W32_Handler
  112.         
  113.       ; calculate real handler address from the stub address
  114.       mov     esi, [edx].Stub_targ
  115.       add     esi, 5
  116.       add     esi, edx
  117.         
  118.       .IF esi == hookproc
  119.          push    [edx].Stub_prev
  120.          pop     [eax].W32_Handler
  121.       .ELSE
  122.          ; traverse the chain of stubs until hookproc is found
  123.          .REPEAT
  124.             ; sanity check
  125.             .IF [edx].Stub_Jmp != JMP_JV
  126.                ; ??? something horrible has happened
  127.                xor     eax, eax                    
  128.                .BREAK
  129.             .ENDIF
  130.  
  131.             ; get previously registered stub
  132.             mov     ecx, [edx].Stub_prev
  133.  
  134.             ; calculate real handler address from the stub address
  135.             mov     esi, [ecx].Stub_targ
  136.             add     esi, 5
  137.             add     esi, ecx
  138.             
  139.             .IF esi == hookproc
  140.                ; ecx -> stub to hookproc
  141.                push    [ecx].Stub_prev
  142.                pop     [edx].Stub_prev
  143.                mov     edx, ecx
  144.                .BREAK
  145.             .ENDIF              
  146.  
  147.             mov     edx, ecx            
  148.          .UNTIL eax != eax   ; forever
  149.       .ENDIF  
  150.       VMMCall _HeapFree,<edx,0>
  151.    .ENDIF
  152.     
  153.    mov     [esp].Pushad_EAX, eax
  154.    popad
  155.    ret
  156. Unhook_Win32_Service endp
  157.  
  158. ; for each VxD in the DDB chain that provides Win32 services
  159. ; call enumeration procedure with pDDB and pWin32SvcTab
  160. Enumerate_Win32_Services proc near C public,enumproc:dword
  161. local   ddb:PDDB
  162. local   id:dword
  163.  
  164.    pushad
  165.     
  166.    ; get the root of the DDB chain
  167.    VMMCall VMM_GetDDBList
  168.    .WHILE eax
  169.       .IF [eax].DDB_Flags & DDB_HAS_WIN32_SVCS
  170.          mov     edx, [eax].DDB_Win32_Service_Table
  171.          .IF edx
  172.             ; save ddb
  173.             mov     ddb, eax
  174.  
  175.             ; store shifted VxD ID
  176.             movzx   eax, [eax].DDB_Req_Device_Number
  177.             shl     eax, 16
  178.             mov     id, eax
  179.             ; start with Win32 service 0
  180.             xor     ecx, ecx
  181.                 
  182.             ; call enumproc back for every Win32 service
  183.             .WHILE ecx < [edx]
  184.                push    edx
  185.                push    ecx
  186.  
  187.                or      ecx, id
  188.                cCall   enumproc,<ecx>
  189.                or      eax, eax
  190.                     
  191.                pop     ecx
  192.                pop     edx
  193.                     
  194.                .BREAK .IF !ZERO?
  195.                inc     ecx
  196.             .ENDW
  197.             ; restore ddb
  198.             mov     eax, ddb
  199.          .ENDIF          
  200.       .ENDIF
  201.       ; get next in the DDB chain
  202.       mov     eax, [eax].DDB_Next
  203.    .ENDW
  204.  
  205.    popad
  206.    ret
  207. Enumerate_Win32_Services endp
  208.  
  209. VxD_CODE_ENDS
  210.    end
  211.